package com.aerfafish.spider.neo4j.spider.service.impl;

import com.aerfafish.spider.neo4j.dao.FundHoldShare;
import com.aerfafish.spider.neo4j.repository.FundHoldShareRepository;
import com.aerfafish.spider.neo4j.repository.FundRepository;
import com.aerfafish.spider.neo4j.repository.ShareRepository;
import com.aerfafish.spider.neo4j.spider.service.FundShareRelationService;
import org.neo4j.driver.exceptions.TransientException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author aerfafish
 * @date 2021/4/21 1:56 下午
 */

@Service
public class FundShareRelationServiceImpl implements FundShareRelationService {

    private final static Logger logger = LoggerFactory.getLogger(FundShareRelationServiceImpl.class);

    @Autowired
    FundHoldShareRepository fundHoldShareRepository;

    @Autowired
    FundRepository fundRepository;

    @Autowired
    ShareRepository shareRepository;

    @Override
    public void save(FundHoldShare fundHoldShare) throws Exception {
        save(fundHoldShare, fundHoldShare.getFund().getCode(), fundHoldShare.getShare().getCode());
    }

    @Override
    public void save(FundHoldShare fundHoldShare, String fundCode, String shareCode) throws Exception {
        save(fundCode, shareCode, fundHoldShare.getProportion(), fundHoldShare.getSharesNum(),
                fundHoldShare.getSharesValue());
    }

    @Override
    public void save(String fundCode, String shareCode, Float proportion, Float sharesNum, Float sharesValue) throws Exception {
        if (fundCode == null || shareCode == null || proportion == null || sharesNum == null || sharesValue == null) {
            logger.error("some parameter missing");
            return;
        }
        Pattern pattern = Pattern.compile("^[^0-9]*([0-9]*$)");
        Matcher matcher = pattern.matcher(shareCode);
        // 去掉前面的字母，也就是市场名字
        if (matcher.find()) {
            shareCode = matcher.group(1);
        } else {
            throw new Exception("save FundShare: match share code error");
        }
        try {
            fundHoldShareRepository.save(fundCode, shareCode, proportion, sharesNum, sharesValue);
        } catch (TransientException | InvalidDataAccessApiUsageException e) {
            try {
                fundHoldShareRepository.save(fundCode, shareCode, proportion, sharesNum, sharesValue);
            } catch (TransientException | InvalidDataAccessApiUsageException e0) {
                fundHoldShareRepository.save(fundCode, shareCode, proportion, sharesNum, sharesValue);
            }
        }


//        if (fundHoldShares.size() > 1) {
//            List<Fund> funds = fundRepository.queryFundByCode(fundCode);
//            if (funds.size() > 1) {
//                String fundsCode = "";
//                for (Fund fund : funds) {
//                    fundsCode = fundsCode.concat(fund.getCode() + " ");
//                }
//                throw new Exception("fundCode repeated " + fundsCode);
//            }
//            List<Share> shares = shareRepository.queryByShareCode(shareCode);
//            if (shares.size() > 1) {
//                String sharesCode = "";
//                for (Share share : shares) {
//                    shareCode = shareCode.concat(share.getCode() + " ");
//                }
//                throw new Exception("shareCode repeated " + shareCode);
//            }
//        }
    }

    @Override
    public List<FundHoldShare> queryRiskFund() {
        return fundHoldShareRepository.queryRiskFund();
    }
}
